package org.ala.tiktools.generator.mybatis.tools;

import java.io.File;
import java.math.BigDecimal;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Date;

/**
 * 相关工具
 *
 * @author ala
 * @date 2024-12-31 12:38
 */
public class Tools {


    /**
     * 完整类名中解析出类名
     */
    public static String parseClassName(String name) {
        String[] ss = name.split("\\.");
        return ss[ss.length - 1];
    }

    /**
     *  驼峰 + 首字母大写
     */
    public static String upHumpParseName(String name) {
        return lowToUpFirst(hump(parseClassName(name)));
    }
    /**
     *  驼峰 + 首字母大写
     */
    public static String upHump(String name) {
        return lowToUpFirst(hump(name));
    }
    /**
     *  驼峰命名
     */
    public static String hump(String name) {
        char[] cs = name.toCharArray();
        cs[0] = upToLow(cs[0]);
        //  去掉下划线，并且下划线后的第一个字母大写
        int j = 0;
        for (int i = 0 ; i < cs.length ; i++) {
            if (cs[i] == '_') {
                cs[j++] = lowToUp(cs[++i]);
            } else {
                cs[j++] = cs[i];
            }
        }
        return new String(cs, 0, j);
    }
    public static char upToLow(char c) {
        return c >= 'A' && c <= 'Z' ? (char)(c + ('a' - 'A')) : c;
    }
    public static char lowToUp(char c) {
        return c >= 'a' && c <= 'z' ? (char)(c - ('a' - 'A')) : c;
    }
    public static String lowToUpFirst(String str) {
        char[] cs = str.toCharArray();
        cs[0] = lowToUp(cs[0]);
        return new String(cs);
    }


    /**
     *  get 方法名
     *  <p> name必须是驼峰命名后的
     */
    public static String getMethodName(String name) {
        char[] cs = name.toCharArray();
        cs[0] = lowToUp(cs[0]);
        return String.format("get%s", new String(cs));
    }
    /**
     *  set 方法名
     *  <p> name必须是驼峰命名后的
     */
    public static String setMethodName(String name) {
        char[] cs = name.toCharArray();
        cs[0] = lowToUp(cs[0]);
        return String.format("set%s", new String(cs));
    }


    /**
     *  数据库字段类型与java数据类型转换
     */
    public static FieldType javaType(String mysqlType) {
        Class<?> type = null;
        String typeName = null;
        switch (mysqlType) {
            case "VARCHAR":
            case "CHAR":
            case "TEXT": {type = String.class; break;}

            case "BLOB": {typeName = "byte[]"; break;}

            case "INTEGER":
            case "INT": {type = Integer.class; break;}

            case "BIGINT":
            case "ID": {type = Long.class; break;}

            case "TINYINT":
            case "SMALLINT":
            case "BOOLEAN":
            case "MEDIUMINT": {type = Integer.class; break;}

            case "BIT": {type = Integer.class; break;}

            case "FLOAT": {type = Float.class; break;}
            case "DOUBLE": {type = Double.class; break;}
            case "DECIMAL": {type = BigDecimal.class; break;}

            case "YEAR":
            case "DATE": {type = Date.class; break;}
            case "TIME": {type = Time.class; break;}
            case "DATETIME":
            case "TIMESTAMP": {type = Timestamp.class; break;}
            default: {type = String.class; break;}
        }
        return FieldType.build(type, typeName);
    }

    /**
     *  检测目录是否存在，不存在则创建
     */
    public static boolean existsAndMakeDirs(String dir) {
        File d = new File(dir);
        if (!d.exists()) {
            return d.mkdirs();
        }
        return true;
    }
}
